import { useQuery } from "react-query";

function fetchPosts() {
  return fetch("https://example.com/posts").then((res) => res.json());
}

export function PostList() {
  const { data, isLoading, error } = useQuery("posts", fetchPosts);

  if (isLoading) return <div>加载中...</div>;
  if (error) return <div>哎呀！出错了</div>;

  return (
    <ul>
      {data.map((post) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  );
}
